package com.cmccgz.udacs_landlord_webapp.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.cmccgz.baselibrary.base.BaseHolder;
import com.cmccgz.baselibrary.base.NoDataViewHolder;
import com.cmccgz.udacs_landlord_webapp.R;
import com.cmccgz.udacs_landlord_webapp.bean.HouseListDataBean;
import com.cmccgz.udacs_landlord_webapp.main.housemanager.function.HouseFunctionActivity;
import com.cmccgz.udacs_landlord_webapp.main.housemanager.list.HouseListActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: Huang
 * @Descripion:
 * @date 2017/6/6 17:29
 */

public class HouseListAdapter extends RecyclerView.Adapter<BaseHolder> {

    private Context context;
    private List<HouseListDataBean> data;
    private int type;
    private boolean isForSelected;

    public HouseListAdapter(Context context, int type, boolean isForSelected) {
        this.context = context;
        this.type = type;
        this.isForSelected = isForSelected;
    }

    @Override
    public int getItemViewType(int position) {
        if (data == null){
            return -1;
        }else if (data.size() == 0){
            return 0;
        }else {
            return 1;
        }
    }

    @Override
    public BaseHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 0){
            return new NoDataViewHolder(LayoutInflater.from(context).inflate(R.layout.lv_empty_view,parent,false));
        }else if (viewType == 1){
            return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_house_list, parent, false));
        }else {
            return null;
        }
    }

    @Override
    public void onBindViewHolder(BaseHolder holder, int position) {
        holder.instanceView(position);
    }


    @Override
    public int getItemCount() {
        if (data == null){
            return 0;
        }else if (data.size() == 0){
            return 1;
        }else {
            return data.size();
        }
    }

    private class ViewHolder extends BaseHolder {

        private ImageView icon;
        private TextView name;
        private ImageView iv_more;

        public ViewHolder(View itemView) {
            super(itemView);
        }

        @Override
        public void assignViews() {
            icon = (ImageView) findViewById(R.id.iv_icon);
            name = (TextView) findViewById(R.id.tv_name);
            iv_more = (ImageView) findViewById(R.id.iv_more);
        }

        @Override
        public void instanceView(int position) {
            if (data!=null) {
                final HouseListDataBean houseListDataBean = data.get(position);
                switch (type) {
                    case HouseListActivity.TYPE_CHECKOUT:
                        icon.setImageResource(R.mipmap.icon_tuifang_list);
                        break;
                    case HouseListActivity.TYPE_EXCHANGE:
                        icon.setImageResource(R.mipmap.icon_huanfang_list);
                        break;
                    case HouseListActivity.TYPE_ROOM_COUNT:
                        icon.setImageResource(R.mipmap.icon_count_list);
                        break;
                    case HouseListActivity.TYPE_SELECT_HOUSE:
                    case HouseListActivity.TYPE_SELECT_BUILDING:
                        icon.setImageResource(R.mipmap.icon_count_list);
                        break;
                    default:
                        break;
                }
                name.setText(houseListDataBean.getName());
                if (isForSelected) {
                    iv_more.setVisibility(View.INVISIBLE);
                } else {
                    iv_more.setVisibility(View.VISIBLE);
                }
                getItemView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (isForSelected) {
                            HouseListActivity activity = (HouseListActivity) context;
                            Intent intent = new Intent();
                            intent.putExtra(HouseListActivity.HOUSE_NAME, houseListDataBean.getName());
                            intent.putExtra(HouseListActivity.HOUSE_ID, houseListDataBean.getId());
                            activity.setResult(HouseListActivity.RESULT_CODE, intent);
                            activity.finish();
                        } else {
                            Intent intent = new Intent(context, HouseFunctionActivity.class);
                            intent.putExtra(HouseListActivity.TYPE, type);
                            intent.putExtra(HouseListActivity.HOUSE_NAME, houseListDataBean.getName());
                            intent.putExtra(HouseListActivity.HOUSE_ID, houseListDataBean.getId());
                            HouseListActivity activity = (HouseListActivity) context;
                            activity.startActivityForResult(intent, HouseListActivity.FINISH_CODE);
                        }
                    }
                });
            }
        }
    }

    public void notify(List<HouseListDataBean> list) {
        if (data == null){
            data = new ArrayList<>();
        }
        this.data.clear();
        if (list != null && list.size() > 0) {
            this.data.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void notifyMore(List<HouseListDataBean> list) {
        if (data == null){
            data = new ArrayList<>();
        }
        if (list != null && list.size() > 0) {
            this.data.addAll(list);
        }
        notifyDataSetChanged();
    }
}
